package com.example.sneakerappserver.service;

import com.example.sneakerappserver.pojo.User;
import com.example.sneakerappserver.pojo.dto.UserDto;
import com.example.sneakerappserver.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service // spring的 bean
public class UserService implements IUserService{

    @Autowired
    UserRepository userRepository;

//    @Override
//    public User add(UserDto user) {
//        // 调用数据访问类
//        User userPojo = new User();
//        BeanUtils.copyProperties(user,userPojo);
//        // save 操作的是User pojo类型 而不是 UserDto
//        return userRepository.save(userPojo); //会返回User对象 save user有Id,修改操作；没有Id,新增操作
//    }

//    @Override
//    public User add() {
//        return null;
//    }

    @Override
    public User queryByUserId(Long id) {
        return userRepository.findByUserId(id);
    }

    @Override
    public User queryByUserName(String userName) {
        return userRepository.findByUserName(userName); // 根据 userName进行查询
    }

    @Override
    public boolean register(String userName, String passWord) {
        User user = new User();
        user.setUserName(userName);
        user.setPassword(passWord);
        user.setRole(0);
        userRepository.save(user);
        return true;
    }


    // 查询用户

}
